package com.xiaoc.bulldozer.facade.impl;

import com.xc.framework.common.exception.BusinessException;
import com.xc.framework.common.thread.ThreadContext;
import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.ConfigKeys;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.WeixinUserDto;
import com.xiaoc.bulldozer.common.model.WeixinUser;
import com.xiaoc.bulldozer.common.service.AccountService;
import com.xiaoc.bulldozer.common.service.WeixinUserService;
import com.xiaoc.bulldozer.common.service.cache.CommonConfigCacheService;
import com.xiaoc.bulldozer.common.util.SessionIdGenerateUtil;
import com.xiaoc.bulldozer.facade.WeixinUserFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("weixinUserFacade")
public class WeixinUserFacadeImpl implements WeixinUserFacade {

    @Resource(name = "weixinUserService")
    private WeixinUserService weixinUserService;

    @Resource(name = "accountService")
    private AccountService accountService;

    @Resource(name = "commonConfigService")
    private CommonConfigCacheService commonConfigService;


    @Override
    public WeixinUserDto getWeixinUserDtoBySessionId(String sessionId) {
        return weixinUserService.getWeixinUserDtoBySessionId(sessionId);
    }

    @Override
    public void putWeixinUserDto(String sessionId, WeixinUserDto weixinUserDto) {
        weixinUserDto.setSessionId(sessionId);
        weixinUserService.putWeixinUserDto(sessionId, weixinUserDto);
    }

    @Override
    public String generateSessionId(String openId) {
        return SessionIdGenerateUtil.generateSessionId(openId);
    }

    @Override
    public WeixinUserDto createWeixinUserDto(String openId, String sessionKey) {

        if (StringUtils.isEmpty(openId)) {
            throw new BusinessException(ResponseCode.BUSINESS_ERROR.getCode(), ResponseCode.BUSINESS_ERROR.getMsg());
        }

        String sessionId = generateSessionId(openId);

        WeixinUser weixinUser = weixinUserService.getWeixinUserByOpenId(openId);

        long currentTime = System.currentTimeMillis();

        // 微信用户不存在时, 创建微信用户; 存在时, 重新放置到缓存中
        // double check
        if (weixinUser == null) {
            weixinUser = new WeixinUser();

            weixinUser.setOpenId(openId);
            weixinUser.setUserType(WeixinUser.TYPE_USER);
            weixinUser.setStatus(Constant.ENABLE);
            weixinUser.setAvatarUrl(commonConfigService.getValue(ConfigKeys.DEFAULT_WX_HEADER_URL));
            weixinUser.setNickName(Constant.DEFAULT_WX_NICKNAME);
            weixinUser.setCreateTime(currentTime);
            weixinUser.setUpdateTime(currentTime);
            weixinUserService.save(weixinUser);
        }

        WeixinUserDto weixinUserDto = BeanUtil.copyProperties(WeixinUserDto.class, weixinUser);
        weixinUserDto.setSessionKey(sessionKey);
        this.putWeixinUserDto(sessionId, weixinUserDto);
        return weixinUserDto;
    }


    @Override
    public void updateWeixinUserInfo(WeixinUserDto weixinUserDto) {

        if (weixinUserDto == null) {
            return;
        }

        String sessionId = weixinUserDto.getSessionId();
        String sessionKey = weixinUserDto.getSessionKey();

        if (StringUtils.isEmpty(sessionId)) {
            sessionId = generateSessionId(weixinUserDto.getOpenId());
        }

        WeixinUser existWeixinUser = weixinUserService.getWeixinUserByOpenId(weixinUserDto.getOpenId());

        if (existWeixinUser == null) {
            throw new BusinessException(ResponseCode.WEIXIN_USER_NOTEXIST.getCode(), ResponseCode.WEIXIN_USER_NOTEXIST.getMsg());
        }

        existWeixinUser.setNickName(weixinUserDto.getNickName());
        existWeixinUser.setGender(weixinUserDto.getGender());
        existWeixinUser.setCity(weixinUserDto.getCity());
        existWeixinUser.setProvince(weixinUserDto.getProvince());
        existWeixinUser.setCountry(weixinUserDto.getCountry());
        existWeixinUser.setAvatarUrl(weixinUserDto.getAvatarUrl());
//        existWeixinUser.setUnionId(weixinUserDto.getUnionId());
        existWeixinUser.setUpdateTime(System.currentTimeMillis());
        existWeixinUser.setUserType(weixinUserDto.getUserType());
        weixinUserService.update(existWeixinUser);

        weixinUserDto = BeanUtil.copyProperties(WeixinUserDto.class, existWeixinUser);
        weixinUserDto.setSessionKey(sessionKey);
        this.putWeixinUserDto(sessionId, weixinUserDto);
    }

    @Override
    public void updateWeixinUserAddress(WeixinUserDto weixinUserDto) {

        if (weixinUserDto == null) {
            return;
        }

        String sessionId = weixinUserDto.getSessionId();

        if (StringUtils.isEmpty(sessionId)) {
            sessionId = generateSessionId(weixinUserDto.getOpenId());
        }

        WeixinUser existWeixinUser = weixinUserService.getWeixinUserByOpenId(weixinUserDto.getOpenId());

        if (existWeixinUser == null) {
            throw new BusinessException(ResponseCode.WEIXIN_USER_NOTEXIST.getCode(), ResponseCode.WEIXIN_USER_NOTEXIST.getMsg());
        }

        existWeixinUser.setLocationAddress(weixinUserDto.getLocationAddress());

        existWeixinUser.setUpdateTime(System.currentTimeMillis());
        weixinUserService.update(existWeixinUser);

        weixinUserDto = BeanUtil.copyProperties(WeixinUserDto.class, existWeixinUser);

        this.putWeixinUserDto(sessionId, weixinUserDto);
    }


    @Override
    public WeixinUserDto getWeixinUserDtoByOpenId(String openId) {

        WeixinUser weixinUser = weixinUserService.getWeixinUserByOpenId(openId);

        return BeanUtil.copyProperties(WeixinUserDto.class, weixinUser);
    }

    public String getCurrentSessionId() {
        return (String) ThreadContext.get(Constant.COOKIE_SESSION_ID);
    }

    @Override
    public WeixinUserDto getCurrentWeixinUserDto() {
        return getWeixinUserDtoBySessionId(getCurrentSessionId());
    }

}
